﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebSite.Core;
using Common;
using Maticsoft.Model.CarrefourCover;

namespace WebSite
{
    public partial class BabyGroup1112 : BasePage
    {
        public override bool NeedLogin
        {
            get
            {
                return false;
            }
        }


        // public int currentTeamID = 0;
        public Maticsoft.Model.CarrefourCover.Baby babyA = null;
        public string babyAHeadImage = null;
        public string babyBHeadImage = null;
        public string babyALiveImage1 = null;
        public string babyALiveImage2 = null;
        public string babyBLiveImage1 = null;
        public string babyBLiveImage2 = null;
        public Maticsoft.Model.CarrefourCover.Baby babyB = null;
        public Maticsoft.Model.CarrefourCover.Team curTeam = null;

        public List<Maticsoft.Model.CarrefourCover.Message> listMessage = null;
        public List<Maticsoft.Model.CarrefourCover.Image> listImage = null;

        public bool IsTheFirst { get; set; }    //是否冠军
        public int TheFirstTeamId = 655;

        public int currentTeamID = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            Maticsoft.BLL.CarrefourCover.Team teambll = new Maticsoft.BLL.CarrefourCover.Team();
            Maticsoft.BLL.CarrefourCover.Baby babybll = new Maticsoft.BLL.CarrefourCover.Baby();

            NavHelper.SetNavMenuSelectedIndex(1);
            NavHelper.SetNavStepCurrentStep(3);

            Project_Session.RefreshBabyInfo();

            /*if (Project_Session.CurrentUserBaby == null)
            {                
                AlertMessage("决赛进行中，去给晋级宝宝帮投票吧", "/SubmitInfo.aspx");
                return;
            }*/

            /*if (Project_Session.CurrentUserBaby == null)
            {
                AlertMessage("您尚未填写宝宝资料", "/SubmitInfo.aspx");
            }*/

            currentTeamID = StringHelper.Convert(Request["teamid"], 0);
            int curTeamID;
            string backUrl;
            if (currentTeamID == 0)
            {
                curTeamID = NullableHelper.CastValueType(Project_Session.CurrentUserBaby.ConfirmTeamId);
                backUrl = "BabyGroup.aspx";
            }
            else
            {
                curTeamID = currentTeamID;
                backUrl = "ViewBabyGroup.aspx?teamid=" + currentTeamID;                
            }

            curTeam = teambll.GetModel(curTeamID);
            if (curTeam == null)
            {
                AlertMessage("组队不存在", "/index.aspx");
            }
            if (curTeam.Type < 2)
            {
                Response.Redirect(backUrl);
            }

            if (TheFirstTeamId == currentTeamID)
            {
                IsTheFirst = true;
            }
            else
            {
                IsTheFirst = false;
            }

            //int curBabyID = Project_Session.CurrentUserBaby.Id;
              
            babyA = babybll.GetModel(curTeam.BabyAId.Value);
            babyB = babybll.GetModel(curTeam.BabyBId.Value);
            NavHelper.SetNavStepCurrentStep(6);
               
          

            // 获取生活照
            Maticsoft.BLL.CarrefourCover.Image imgbll = new Maticsoft.BLL.CarrefourCover.Image();
            string emptyImage = "/images/BabyGroup/waitselect_200.png";

            if (currentTeamID == 0)
            {
                //只显示自己
                int curBabyID = Project_Session.CurrentUserBaby.Id;
                listImage = imgbll.GetLiveImageByBabyID(curBabyID);
                if (listImage != null)
                {
                    for (int i = listImage.Count; i < 3; i++)
                    {
                        listImage.Add(new Maticsoft.Model.CarrefourCover.Image() { ImgUrl = emptyImage, Id = 0 });
                    }
                }
            }
            else
            {
                //显示两个队伍的                
                var listImage0 = imgbll.GetLiveImageByBabyID(babyA.Id);
                var listImage1 = imgbll.GetLiveImageByBabyID(babyB.Id);
                listImage = new List<Maticsoft.Model.CarrefourCover.Image>();
                if (listImage0 != null)
                {
                    for (int i = 0; i < listImage0.Count; i++)
                    {
                        listImage.Add(new Maticsoft.Model.CarrefourCover.Image() { ImgUrl = listImage0[i].ImgUrl, Id = listImage0[i].Id });
                    }
                }
                if (listImage1 != null)
                {
                    for (int i = 0; i < listImage1.Count; i++)
                    {
                        listImage.Add(new Maticsoft.Model.CarrefourCover.Image() { ImgUrl = listImage1[i].ImgUrl, Id = listImage1[i].Id });
                    }
                }
                if (listImage != null)
                {
                    for (int i = listImage.Count; i < 3; i++)
                    {
                        listImage.Add(new Maticsoft.Model.CarrefourCover.Image() { ImgUrl = emptyImage, Id = 0 });
                    }
                }
            }

       
            if (curTeamID != 0)
            {
                Maticsoft.BLL.CarrefourCover.Message bllmsg = new Maticsoft.BLL.CarrefourCover.Message();
                listMessage = bllmsg.GetModelList("ReceviedTeamId=" + curTeamID).OrderByDescending(p => p.CreateTime).Take(100).ToList();
                // listMessage
            }
            else
            {
                listMessage = new List<Maticsoft.Model.CarrefourCover.Message>();
            }
        }
    }
}